package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/7/27 16:02
 *
 * 商品类目表
 */
@Service
public class CategoryServiceImpl implements  CategoryService{

   @Autowired
   private DtsCategoryMapper categoryMapper;

    @Override
    public IPage<DtsCategory> queryByCategory(Integer page, Integer limit, String sort, String order, Integer id, String name) {

       //分页对象 ,第一参数:当前页,第二参数:每页显示条数
        IPage<DtsCategory> ipage = new Page<>(page, limit);

        //创建对象
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        //判断查询条件是否为空
        if (id !=null){
            queryWrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name", name);
        }

        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
           if (order.equalsIgnoreCase("asc")){
               queryWrapper.orderByDesc(sort);
           }else {
               queryWrapper.orderByAsc(sort);
           }
        }
        ipage = categoryMapper.selectPage(ipage, queryWrapper);

        return ipage;
    }

    /**
     * 添加
     * @param category
     * @return
     */
    @Override
    public DtsCategory add(DtsCategory category) {
        categoryMapper.insert(category);
        return category;
    }


    /**
     * 删除
     * @param category
     */
    @Override
    public int delete(DtsCategory category) {
        int delete = categoryMapper.deleteById(category);
        return delete;
    }


    /**
     * 修改
     * @param category
     * @return
     */
    @Override
    public int update(DtsCategory category) {
        int update = categoryMapper.updateById(category);
        return update;
    }

    /**
     * 下拉
     */
    @Override
    public List<CatVo> findCat() {
        List<CatVo>  findCat = categoryMapper.findCat();
        for (CatVo catVo : findCat) {
            Integer id = catVo.getValue();
            List<CatVo> children = categoryMapper.findChildren(id);
            catVo.setChildren(children);
        }

        return findCat;
    }
}
